import defaultSetting from "@/common/utils/defaultSetting";
import utils from "@/common/utils/utils";
import { message } from "ant-design-vue";
import { streamFetch } from "@/common/utils/streamFetch";

let BASE_URL = defaultSetting.requestDomain;
if (process.env.NODE_ENV === "development") {
    BASE_URL = BASE_URL + "/api";
}


export const request = async (text, type, cb) => {
    let params = {
        user_prompt: text,
        type,
        org_id: utils.getOrgId(),
        user_token: utils.getToken(),
        cur_env: utils.isMobile()
      ? "h5"
      : import.meta.env.ELECTRON
        ? "desktop"
        : "pc"
    }

    const { responseText } = await streamFetch({
        url: `${BASE_URL}/work_ai//chat/text_build`,
        data: params,
        onMessage: (text) => {
            if (utils.isJSONStr(text)) {
                let res = JSON.parse(text);

                if (utils.isObject(res) && res.msg) {
                    message.error(res.msg);
                    return;
                }
            }

            cb && cb(text)
        },
    }).catch((err) => {
        console.log(err);

        return {
            responseText: '',
        };
    })

    return responseText
}